package com.education.ai.service.impl;

import com.education.ai.entity.TeachingPlan;
import com.education.ai.repository.TeachingPlanRepository;
import com.education.ai.service.TeachingPlanService;
import com.education.ai.util.TeacherContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class TeachingPlanServiceImpl implements TeachingPlanService {

    @Autowired
    private TeachingPlanRepository teachingPlanRepository;

    @Override
    public List<TeachingPlan> findAll() {
        return teachingPlanRepository.findAll();
    }
    
    @Override
    public List<TeachingPlan> findAll(Pageable pageable) {
        return teachingPlanRepository.findAll(pageable).getContent();
    }

    @Override
    public TeachingPlan findById(Long id) {
        return teachingPlanRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("未找到教学计划，ID: " + id));
    }

    @Override
    @Transactional
    public TeachingPlan save(TeachingPlan teachingPlan) {
        // 设置关联的教师ID
        String currentTeacherId = TeacherContext.getTeacherId();
        if (currentTeacherId != null && (teachingPlan.getTeacherId() == null || teachingPlan.getTeacherId().isEmpty())) {
            log.info("为教学计划关联教师ID: {}", currentTeacherId);
            teachingPlan.setTeacherId(currentTeacherId);
        }
        
        return teachingPlanRepository.save(teachingPlan);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        teachingPlanRepository.deleteById(id);
    }

    @Override
    public List<TeachingPlan> findBySubject(String subject) {
        return teachingPlanRepository.findBySubject(subject);
    }

    @Override
    public List<TeachingPlan> findByGrade(String grade) {
        return teachingPlanRepository.findByGrade(grade);
    }

    @Override
    public List<TeachingPlan> findTemplates() {
        return teachingPlanRepository.findByIsTemplateTrue();
    }
    
    @Override
    public List<TeachingPlan> findByTeacherId(String teacherId) {
        return teachingPlanRepository.findByTeacherIdOrderByCreateTimeDesc(teacherId);
    }
    
    @Override
    public List<TeachingPlan> findByTeacherId(String teacherId, Pageable pageable) {
        Specification<TeachingPlan> spec = (root, query, cb) -> cb.equal(root.get("teacherId"), teacherId);
        return teachingPlanRepository.findAll(spec, pageable).getContent();
    }
    
    @Override
    public long count() {
        return teachingPlanRepository.count();
    }
    
    @Override
    public long countByTeacherId(String teacherId) {
        Specification<TeachingPlan> spec = (root, query, cb) -> cb.equal(root.get("teacherId"), teacherId);
        return teachingPlanRepository.count(spec);
    }
} 